/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.service.wms;

import java.awt.Image;
import java.io.IOException;
import java.io.PrintStream;

import chunmap.model.elem.Envelope;
import chunmap.util.image.ImageUtil;
import chunmap.view.Map;

/**
 * @author yangjiandong
 * 
 */
public class GetMap {

	private Envelope envelop;
	private Capabilities capbilities;
	private int height;
	private int width;
	private String formatName = "png";

	public GetMap(Capabilities capbilities) {
		this.capbilities = capbilities;
	}

	public void reponse(PrintStream out) throws IOException {
		byte[] data = createData();
		out.write(data);
	}

	private byte[] createData() throws IOException {
		// create map
//		View view = new ViewPort(width, height,3);
//		Painter painter = new Painter(width, height);
		Map map = new Map(width, height);
		map.getLayerCollection().setLayers(capbilities.getLayers());

		// getImage
		map.getView().setViewEnvelop(envelop);
		map.render();
		Image image = (Image)map.getPainter().getData();

		// toByteArray
		return ImageUtil.Image2ByteArray(image, formatName);
	}

	// -------------------------------------------------------------------------

	public Envelope getEnvelop() {
		return envelop;
	}
	public void setEnvelop(Envelope envelop) {
		this.envelop = envelop;
	}
	public Capabilities getCapbilities() {
		return capbilities;
	}
	public void setCapbilities(Capabilities capbilities) {
		this.capbilities = capbilities;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public String getFormatName() {
		return formatName;
	}
	public void setFormatName(String formatName) {
		this.formatName = formatName;
	}
}
